﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Packet.InPacket
{
    /// <summary>
    ///     添加用户分组包
    /// </summary>
    class AddItemPacket : BaseInPackage
    {
        public AddItemPacket(InPackage inpackage) : base(inpackage)
        {
            
        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //读取用户名
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //得到分组名称长度
            byte[] ItemNameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemNameLength, 0, 4);
            Offset += 4;
            //得到分组名称
            byte[] ItemName = new byte[BitConverter.ToInt32(ItemNameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemName, 0, ItemName.Length);
            Offset += ItemName.Length;
            //得到分组KEY长度
            byte[] ItemKeyLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemKeyLength, 0, 4);
            Offset += 4;
            //得到分组KEY
            byte[] ItemKey = new byte[BitConverter.ToInt32(ItemKeyLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemKey, 0, ItemKey.Length);
            Offset += ItemKey.Length +2;
            //IP地址长度
            byte[] IPLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPLength, 0, 4);
            Offset += 4;
            //IP地址
            byte[] IPAddress = new byte[BitConverter.ToInt32(IPLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPAddress, 0, IPAddress.Length);
            Offset += IPAddress.Length;
            //聊天端口
            byte[] Talkport = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Talkport, 0, 4);

            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("VALUE", ItemName);
            this.DIC_CONTENT.Add("ITEMKEY", ItemKey);
            this.DIC_CONTENT.Add("IPADDRESS", IPAddress);
            this.DIC_CONTENT.Add("TALKPORT", Talkport);
        }

        /// <summary>
        ///     等到分组KEY
        /// </summary>
        /// <returns>返回分组KEY</returns>
        public String GetItemKey()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["ITEMKEY"]);
        }

        /// <summary>
        ///     得到分组新值
        /// </summary>
        /// <returns></returns>
        public String GetValue()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["VALUE"]);
        }
    }
}
